<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Cafebz
{
    protected $ci;
    protected $db;

    public function __construct()
    {
        $this->ci = & get_instance();
        $this->ci->load->model('cafebz_model');

        $this->ci->cafebz = $this;
    }

    public function sf_revenue_daily($time)
    {
        $result = $this->ci->cafebz_model->get_sf_revenue($time);
        if (is_null($result['subtotal'])) {
            return array('subtotal' => 0, 'discount' => 0, 'service' => 0, 'tax' => 0, 'rounding' => 0, 'discount_customer' => 0, 'discount_ocheck' => 0);
        } else {
            return $result;
        }
    }

    public function sf_media_collection_daily($time)
    {
        $result = $this->ci->cafebz_model->get_sf_media_collection($time);
        $data = array(
            'BCA CARD' => array('count' => 0, 'total' => 0),
            'CITY LEDGER' => array('count' => 0, 'total' => 0),
            'CASH' => array('count' => 0, 'total' => 0),
            'VISA' => array('count' => 0, 'total' => 0),
            'DEBIT' => array('count' => 0, 'total' => 0),
            'MASTER' => array('count' => 0, 'total' => 0),
            'VOUCHER' => array('count' => 0, 'total' => 0)
        );
        foreach ($result as $val) {
            switch ($val['category']) {
                case 'BCA CARD': $data['BCA CARD']['count'] = $val['count'];
                    $data['BCA CARD']['total'] = $val['total'];
                    break;
                case 'CITY LEDGER': $data['CITY LEDGER']['count'] = $val['count'];
                    $data['CITY LEDGER']['total'] = $val['total'];
                    break;
                case 'CASH': $data['CASH']['count'] = $val['count'];
                    $data['CASH']['total'] = $val['total'];
                    break;
                case 'VISA': $data['VISA']['count'] = $val['count'];
                    $data['VISA']['total'] = $val['total'];
                    break;
                case 'DEBIT': $data['DEBIT']['count'] = $val['count'];
                    $data['DEBIT']['total'] = $val['total'];
                    break;
                case 'MASTER': $data['MASTER']['count'] = $val['count'];
                    $data['MASTER']['total'] = $val['total'];
                    break;
                case 'VOUCHER': $data['VOUCHER']['count'] = $val['count'];
                    $data['VOUCHER']['total'] = $val['total'];
                    break;
            }
        }
        return $data;
    }

    public function sf_department_daily($time)
    {
        $result = $this->ci->cafebz_model->get_sf_department($time);
        $data = array(
            'BEVERAGE' => array('count' => 0, 'total' => 0),
            'FOOD' => array('count' => 0, 'total' => 0),
            'TOBACO' => array('count' => 0, 'total' => 0),
            'OTHERS' => array('count' => 0, 'total' => 0)
        );
        foreach ($result as $val) {
            switch ($val['category']) {
                case 'BEVERAGE': $data['BEVERAGE']['count'] = $val['count'];
                    $data['BEVERAGE']['total'] = $val['total'];
                    break;
                case 'FOOD': $data['FOOD']['count'] = $val['count'];
                    $data['FOOD']['total'] = $val['total'];
                    break;
                case 'TOBACO': $data['TOBACO']['count'] = $val['count'];
                    $data['TOBACO']['total'] = $val['total'];
                    break;
                default: $data['OTHERS']['count'] += $val['count'];
                    $data['OTHERS']['total'] += $val['total'];
                    break;
            }
        }
        return $data;
    }

    public function sf_sales_daily($time, $return_original = false)
    {
        $result = $this->ci->cafebz_model->get_sf_sales($time);

        // If record is not found, retrive from Quinos database
        if (count($result) == 0 || $return_original) {
            $revenue = $this->sf_revenue_daily($time);
            $department = $this->sf_department_daily($time);
            $net_sales = $department['BEVERAGE']['total'] + $department['FOOD']['total'] + $department['TOBACO']['total'] + $department['OTHERS']['total'] - $revenue['discount'] + $revenue['rounding'];
            $subtotal = $net_sales + $revenue['tax'] + $revenue['service'];
            
            // Round to the nearest 100
            $reminder = $subtotal % 100;
            if($reminder!=0)
            {
                if($reminder <= 50)
                {
                    $revenue['rounding'] -= $reminder;
                    $net_sales -= $reminder;
                    $subtotal -= $reminder;
                }
                else
                {
                    $reminder = 100-$reminder;
                    $revenue['rounding'] += $reminder;
                    $net_sales += $reminder; 
                    $subtotal += $reminder;
                }
            }
            
            $data = array(
                'date' => date('Y-m-d', $time),
                'beverage' => $department['BEVERAGE']['total'],
                'food' => $department['FOOD']['total'],
                'tobaco' => $department['TOBACO']['total'],
                'others' => $department['OTHERS']['total'],
                'discount' => $revenue['discount'],
                'discount_customer' => $revenue['discount_customer'],
                'discount_ocheck' => $revenue['discount_ocheck'],
                'rounding' => $revenue['rounding'],
                'net_sales' => $net_sales,
                'tax' => $revenue['tax'],
                'service' => $revenue['service'],
                'subtotal' => $subtotal
            );

            // if data is earlier than today, store in cafebz database
            if ($time < strtotime('today') && !$return_original) {
                $this->ci->cafebz_model->insert_sf_sales($data);
            }

            $data["modified_by"] = "";
            return $data;
        } else {
            return $result;
        }
    }

    public function sf_sales_monthly($month, $year)
    {
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        
        // Insert data
        $data = array();
        for($i=1; $i<=$days; $i++)
        {
            $time = mktime(0,0,0,$month,$i,$year);
            $data[] = $this->sf_sales_daily($time);
        }
        
        return $data;
    }
    
    public function sf_sales_update($time, $data)
    {
        $discount = $data['discount_customer'] + $data['discount_ocheck'];
        $net_sales = $data['beverage'] + $data['food'] + $data['tobaco'] + $data['others'] - $discount + $data['rounding'];
        $subtotal = $net_sales + $data['tax'] + $data['service'];
        
        $data['discount'] = $discount;
        $data['net_sales'] = $net_sales;
        $data['subtotal'] = $subtotal;
        
        $this->ci->cafebz_model->update_sf_sales($time,$data);
    }

    public function sf_payment_daily($time, $return_original = false)
    {
        $result = $this->ci->cafebz_model->get_sf_payment($time);

        // If record is not found, retrive from Quinos database
        if (count($result) == 0 || $return_original) {
            $payment = $this->sf_media_collection_daily($time);
            
            // Count total
            $total = 0;
            foreach($payment as $val) $total += $val['total'];
            
            $data = array(
                'date' => date('Y-m-d', $time),
                'cash' => $payment['CASH']['total'],
                'visa' => $payment['VISA']['total'],
                'master' => $payment['MASTER']['total'],
                'bca_card' => $payment['BCA CARD']['total'],
                'debit' => $payment['DEBIT']['total'],
                'city_ledger' => $payment['CITY LEDGER']['total'],
                'voucher' => $payment['VOUCHER']['total'],
                'total' => $total
            );

            // if data is earlier than today, store in cafebz database
            if ($time < strtotime('today') && !$return_original) {
                $this->ci->cafebz_model->insert_sf_payment($data);
            }

            $data["modified_by"] = "";
            return $data;
        } else {
            return $result;
        }
    }
    
    public function sf_payment_monthly($month, $year)
    {
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        
        // Insert data
        $data = array();
        for($i=1; $i<=$days; $i++)
        {
            $time = mktime(0,0,0,$month,$i,$year);
            $data[] = $this->sf_payment_daily($time);
        }
        
        return $data;
    }
    
    public function sf_payment_update($time, $data)
    {
        $total = 0;
        foreach($data as $key=>$val) 
            if($key != 'modified_by') $total += $val;
        
        $data['total'] = $total;
        
        $this->ci->cafebz_model->update_sf_payment($time, $data);
    }
    
    public function velpa_revenue_daily($time)
    {
        $result = $this->ci->cafebz_model->get_velpa_revenue($time);
        if (is_null($result['subtotal'])) {
            return array('subtotal' => 0, 'discount' => 0, 'service' => 0, 'tax' => 0, 'rounding' => 0, 'discount_customer' => 0, 'discount_ocheck' => 0);
        } else {
            return $result;
        }
    }
    
    public function velpa_department_daily($time)
    {
        $result = $this->ci->cafebz_model->get_velpa_department($time);
        $data = array(
            'BEVERAGE' => array('count' => 0, 'total' => 0),
            'FOOD' => array('count' => 0, 'total' => 0),
            'TAKE AWAY WINE' => array('count' => 0, 'total' => 0),
            'TOBACO' => array('count' => 0, 'total' => 0),
            'OTHERS' => array('count' => 0, 'total' => 0)
        );
        foreach ($result as $val) {
            switch ($val['category']) {
                case 'WINE':
                case 'BEVERAGE': $data['BEVERAGE']['count'] += $val['count'];
                    $data['BEVERAGE']['total'] += $val['total'];
                    break;
                case 'food band':
                case 'FOOD': $data['FOOD']['count'] += $val['count'];
                    $data['FOOD']['total'] += $val['total'];
                    break;
                case 'TAKE AWAY WINE': $data['TAKE AWAY WINE']['count'] = $val['count'];
                    $data['TAKE AWAY WINE']['total'] = $val['total'];
                    break;
                case 'CIGAR':
                case 'TOBACO': $data['TOBACO']['count'] += $val['count'];
                    $data['TOBACO']['total'] += $val['total'];
                    break;
                default: $data['OTHERS']['count'] += $val['count'];
                    $data['OTHERS']['total'] += $val['total'];
                    break;
            }
        }
        return $data;
    }
    
    public function velpa_media_collection_daily($time)
    {
        $result = $this->ci->cafebz_model->get_velpa_media_collection($time);
        $data = array(
            'BCA CARD' => array('count' => 0, 'total' => 0),
            'CITY LEDGER' => array('count' => 0, 'total' => 0),
            'CASH' => array('count' => 0, 'total' => 0),
            'VISA' => array('count' => 0, 'total' => 0),
            'DEBIT' => array('count' => 0, 'total' => 0),
            'MASTER' => array('count' => 0, 'total' => 0),
            'VOUCHER' => array('count' => 0, 'total' => 0)
        );
        foreach ($result as $val) {
            switch ($val['category']) {
                case 'BCA CARD': $data['BCA CARD']['count'] = $val['count'];
                    $data['BCA CARD']['total'] = $val['total'];
                    break;
                case 'CITY LEDGER': $data['CITY LEDGER']['count'] = $val['count'];
                    $data['CITY LEDGER']['total'] = $val['total'];
                    break;
                case ' CASH': $data['CASH']['count'] = $val['count'];
                    $data['CASH']['total'] = $val['total'];
                    break;
                case 'VISA': $data['VISA']['count'] = $val['count'];
                    $data['VISA']['total'] = $val['total'];
                    break;
                case 'DEBIT MANDIRI':
                case 'DEBIT BCA': $data['DEBIT']['count'] += $val['count'];
                    $data['DEBIT']['total'] += $val['total'];
                    break;
                case 'MASTER': $data['MASTER']['count'] = $val['count'];
                    $data['MASTER']['total'] = $val['total'];
                    break;
                case 'VOUCHER': $data['VOUCHER']['count'] = $val['count'];
                    $data['VOUCHER']['total'] = $val['total'];
                    break;
            }
        }
        return $data;
    }
    
    public function velpa_sales_daily($time, $return_original = false)
    {
        $result = $this->ci->cafebz_model->get_velpa_sales($time);

        // If record is not found, retrive from Quinos database
        if (count($result) == 0 || $return_original) {
            $revenue = $this->velpa_revenue_daily($time);
            $department = $this->velpa_department_daily($time);
            $net_sales = $department['BEVERAGE']['total'] + $department['FOOD']['total'] + $department['TAKE AWAY WINE']['total'] + $department['TOBACO']['total'] + $department['OTHERS']['total'] - $revenue['discount'] + $revenue['rounding'];
            $subtotal = $net_sales + $revenue['tax'] + $revenue['service'];
            
            // Round to the nearest 100
            $reminder = $subtotal % 100;
            if($reminder!=0)
            {
                if($reminder <= 50)
                {
                    $revenue['rounding'] -= $reminder;
                    $net_sales -= $reminder;
                    $subtotal -= $reminder;
                }
                else
                {
                    $reminder = 100-$reminder;
                    $revenue['rounding'] += $reminder;
                    $net_sales += $reminder; 
                    $subtotal += $reminder;
                }
            }
            
            $data = array(
                'date' => date('Y-m-d', $time),
                'beverage' => $department['BEVERAGE']['total'],
                'food' => $department['FOOD']['total'],
                'wine_takeaway' => + $department['TAKE AWAY WINE']['total'],
                'tobaco' => $department['TOBACO']['total'],
                'others' => $department['OTHERS']['total'],
                'discount' => $revenue['discount'],
                'discount_customer' => $revenue['discount_customer'],
                'discount_ocheck' => $revenue['discount_ocheck'],
                'rounding' => $revenue['rounding'],
                'net_sales' => $net_sales,
                'tax' => $revenue['tax'],
                'service' => $revenue['service'],
                'subtotal' => $subtotal
            );

            // if data is earlier than today, store in cafebz database
            if ($time < strtotime('today') && !$return_original) {
                $this->ci->cafebz_model->insert_velpa_sales($data);
            }

            $data["modified_by"] = "";
            return $data;
        } else {
            return $result;
        }
    }

    public function velpa_sales_monthly($month, $year)
    {
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        
        // Insert data
        $data = array();
        for($i=1; $i<=$days; $i++)
        {
            $time = mktime(0,0,0,$month,$i,$year);
            $data[] = $this->velpa_sales_daily($time);
        }
        
        return $data;
    }
    
    public function velpa_sales_update($time, $data)
    {
        $discount = $data['discount_customer'] + $data['discount_ocheck'];
        $net_sales = $data['beverage'] + $data['food'] + $data['wine_takeaway'] + $data['tobaco'] + $data['others'] - $discount + $data['rounding'];
        $subtotal = $net_sales + $data['tax'] + $data['service'];
        
        $data['discount'] = $discount;
        $data['net_sales'] = $net_sales;
        $data['subtotal'] = $subtotal;
        
        $this->ci->cafebz_model->update_velpa_sales($time,$data);
    }
    
    public function velpa_payment_daily($time, $return_original = false)
    {
        $result = $this->ci->cafebz_model->get_velpa_payment($time);

        // If record is not found, retrive from Quinos database
        if (count($result) == 0 || $return_original) {
            $payment = $this->velpa_media_collection_daily($time);
            
            // Count total
            $total = 0;
            foreach($payment as $val) $total += $val['total'];
            
            $data = array(
                'date' => date('Y-m-d', $time),
                'cash' => $payment['CASH']['total'],
                'visa' => $payment['VISA']['total'],
                'master' => $payment['MASTER']['total'],
                'bca_card' => $payment['BCA CARD']['total'],
                'debit' => $payment['DEBIT']['total'],
                'city_ledger' => $payment['CITY LEDGER']['total'],
                'voucher' => $payment['VOUCHER']['total'],
                'total' => $total
            );

            // if data is earlier than today, store in cafebz database
            if ($time < strtotime('today') && !$return_original) {
                $this->ci->cafebz_model->insert_velpa_payment($data);
            }

            $data["modified_by"] = "";
            return $data;
        } else {
            return $result;
        }
    }
    
    public function velpa_payment_monthly($month, $year)
    {
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        
        // Insert data
        $data = array();
        for($i=1; $i<=$days; $i++)
        {
            $time = mktime(0,0,0,$month,$i,$year);
            $data[] = $this->velpa_payment_daily($time);
        }
        
        return $data;
    }
    
    public function velpa_payment_update($time, $data)
    {
        $total = 0;
        foreach($data as $key=>$val) 
            if($key != 'modified_by') $total += $val;
        
        $data['total'] = $total;
        
        $this->ci->cafebz_model->update_velpa_payment($time, $data);
    }
    
    public function cox_revenue_daily($time)
    {
        $result = $this->ci->cafebz_model->get_cox_revenue($time);
        if (is_null($result['subtotal'])) {
            return array('subtotal' => 0, 'discount' => 0, 'service' => 0, 'tax' => 0, 'rounding' => 0, 'discount_customer' => 0, 'discount_ocheck' => 0);
        } else {
            return $result;
        }
    }

    public function cox_payment_method_daily($time)
    {
        $result = $this->ci->cafebz_model->get_cox_payment_method($time);
        $data = array(
            'BCA CARD' => array('count' => 0, 'total' => 0),
            'CITY LEDGER' => array('count' => 0, 'total' => 0),
            'CASH' => array('count' => 0, 'total' => 0),
            'VISA' => array('count' => 0, 'total' => 0),
            'DEBIT' => array('count' => 0, 'total' => 0),
            'MASTER' => array('count' => 0, 'total' => 0),
            'VOUCHER' => array('count' => 0, 'total' => 0)
        );
        foreach ($result as $val) {
            switch ($val['category']) {
                case 'BCA CARD': $data['BCA CARD']['count'] = $val['count'];
                    $data['BCA CARD']['total'] = $val['total'];
                    break;
                case 'City Ledger': $data['CITY LEDGER']['count'] = $val['count'];
                    $data['CITY LEDGER']['total'] = $val['total'];
                    break;
                case 'CASH': $data['CASH']['count'] = $val['count'];
                    $data['CASH']['total'] = $val['total'];
                    break;
                case 'VISA': $data['VISA']['count'] = $val['count'];
                    $data['VISA']['total'] = $val['total'];
                    break;
                case 'DEBIT BCA': $data['DEBIT']['count'] = $val['count'];
                    $data['DEBIT']['total'] = $val['total'];
                    break;
                case 'MASTER CARD': $data['MASTER']['count'] = $val['count'];
                    $data['MASTER']['total'] = $val['total'];
                    break;
                case 'VOUCHER': $data['VOUCHER']['count'] = $val['count'];
                    $data['VOUCHER']['total'] = $val['total'];
                    break;
            }
        }
        return $data;
    }

    public function cox_department_daily($time)
    {
        $result = $this->ci->cafebz_model->get_cox_department($time);
        $data = array(
            'BEVERAGE' => array('count' => 0, 'total' => 0),
            'FOOD' => array('count' => 0, 'total' => 0),
            'TOBACO' => array('count' => 0, 'total' => 0),
            'OTHERS' => array('count' => 0, 'total' => 0)
        );
        foreach ($result as $val) {
            switch ($val['category']) {
                case 'WINES':
                case 'BEVERAGES': $data['BEVERAGE']['count'] += $val['count'];
                    $data['BEVERAGE']['total'] += $val['total'];
                    break;
                case 'FOOD': $data['FOOD']['count'] = $val['count'];
                    $data['FOOD']['total'] = $val['total'];
                    break;
                case 'CIGARS': $data['TOBACO']['count'] = $val['count'];
                    $data['TOBACO']['total'] = $val['total'];
                    break;
                default: $data['OTHERS']['count'] += $val['count'];
                    $data['OTHERS']['total'] += $val['total'];
                    break;
            }
        }
        return $data;
    }

    public function cox_sales_daily($time, $return_original = false)
    {
        $result = $this->ci->cafebz_model->get_cox_sales($time);

        // If record is not found, retrive from Quinos database
        if (count($result) == 0 || $return_original) {
            $revenue = $this->cox_revenue_daily($time);
            $department = $this->cox_department_daily($time);
            $net_sales = $department['BEVERAGE']['total'] + $department['FOOD']['total'] + $department['TOBACO']['total'] + $department['OTHERS']['total'] - $revenue['discount'] + $revenue['rounding'];
            $subtotal = $net_sales + $revenue['tax'] + $revenue['service'];
            
            // Round to the nearest 100
            $reminder = $subtotal % 100;
            if($reminder!=0)
            {
                if($reminder <= 50)
                {
                    $revenue['rounding'] -= $reminder;
                    $net_sales -= $reminder;
                    $subtotal -= $reminder;
                }
                else
                {
                    $reminder = 100-$reminder;
                    $revenue['rounding'] += $reminder;
                    $net_sales += $reminder; 
                    $subtotal += $reminder;
                }
            }
            
            $data = array(
                'date' => date('Y-m-d', $time),
                'beverage' => $department['BEVERAGE']['total'],
                'food' => $department['FOOD']['total'],
                'tobaco' => $department['TOBACO']['total'],
                'others' => $department['OTHERS']['total'],
                'discount' => $revenue['discount'],
                'discount_customer' => $revenue['discount_customer'],
                'discount_ocheck' => $revenue['discount_ocheck'],
                'rounding' => $revenue['rounding'],
                'net_sales' => $net_sales,
                'tax' => $revenue['tax'],
                'service' => $revenue['service'],
                'subtotal' => $subtotal
            );

            // if data is earlier than today, store in cafebz database
            if ($time < strtotime('today') && !$return_original) {
                $this->ci->cafebz_model->insert_cox_sales($data);
            }

            $data["modified_by"] = "";
            return $data;
        } else {
            return $result;
        }
    }

    public function cox_sales_monthly($month, $year)
    {
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        
        // Insert data
        $data = array();
        for($i=1; $i<=$days; $i++)
        {
            $time = mktime(0,0,0,$month,$i,$year);
            $data[] = $this->cox_sales_daily($time);
        }
        
        return $data;
    }
    
    public function cox_sales_update($time, $data)
    {
        $discount = $data['discount_customer'] + $data['discount_ocheck'];
        $net_sales = $data['beverage'] + $data['food'] + $data['tobaco'] + $data['others'] - $discount + $data['rounding'];
        $subtotal = $net_sales + $data['tax'] + $data['service'];
        
        $data['discount'] = $discount;
        $data['net_sales'] = $net_sales;
        $data['subtotal'] = $subtotal;
        
        $this->ci->cafebz_model->update_cox_sales($time,$data);
    }

    public function cox_payment_daily($time, $return_original = false)
    {
        $result = $this->ci->cafebz_model->get_cox_payment($time);

        // If record is not found, retrive from Quinos database
        if (count($result) == 0 || $return_original) {
            $payment = $this->cox_payment_method_daily($time);
            
            // Count total
            $total = 0;
            foreach($payment as $val) $total += $val['total'];
            
            $data = array(
                'date' => date('Y-m-d', $time),
                'cash' => $payment['CASH']['total'],
                'visa' => $payment['VISA']['total'],
                'master' => $payment['MASTER']['total'],
                'bca_card' => $payment['BCA CARD']['total'],
                'debit' => $payment['DEBIT']['total'],
                'city_ledger' => $payment['CITY LEDGER']['total'],
                'voucher' => $payment['VOUCHER']['total'],
                'total' => $total
            );

            // if data is earlier than today, store in cafebz database
            if ($time < strtotime('today') && !$return_original) {
                $this->ci->cafebz_model->insert_cox_payment($data);
            }

            $data["modified_by"] = "";
            return $data;
        } else {
            return $result;
        }
    }
    
    public function cox_payment_monthly($month, $year)
    {
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        
        // Insert data
        $data = array();
        for($i=1; $i<=$days; $i++)
        {
            $time = mktime(0,0,0,$month,$i,$year);
            $data[] = $this->cox_payment_daily($time);
        }
        
        return $data;
    }
    
    public function cox_payment_update($time, $data)
    {
        $total = 0;
        foreach($data as $key=>$val) 
            if($key != 'modified_by') $total += $val;
        
        $data['total'] = $total;
        
        $this->ci->cafebz_model->update_cox_payment($time, $data);
    }
}
?>
